package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.enums.PowerTypeEnum;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.power.PowerResourceSet;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.customer.service.CustomerService;
import com.un.ebs.order.service.OrderService;
import com.un.ebs.product.service.ProductService;
import com.un.ebs.sys.domain.AppPower;
import com.un.ebs.sys.dto.AppPowerAddDto;
import com.un.ebs.sys.dto.AppPowerUpdateDto;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import com.un.ebs.sys.service.AppPowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-18
 */
@RestController
@RequestMapping("/appPower")
@Api(value = "sys-app-power", description = "权限")
public class AppPowerController {

    @Autowired
    private AppPowerService service;

    @Autowired
    private ProductService productService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrderService orderService;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody AppPowerAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody AppPowerUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 AppPower")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "")
    public WebResult list(@RequestParam("appId") String appId, @RequestParam(required = false) Integer type) {
        LambdaQueryWrapper<AppPower> lq = Wrappers.lambdaQuery();
        lq.eq(AppPower::getAppId, appId);
        if (type != null) lq.eq(AppPower::getType, type);
        lq.orderByAsc(AppPower::getSort);
        return WebUtils.success(service.list(lq));
    }

    @PostMapping("changeParent")
    @ApiOperation(value = "changeParent", notes = "修改 AppPower 的 parentId 属性")
    public WebResult changeParent(@RequestParam String id, @RequestParam String parentId) {
        service.changeParent(id, parentId);
        return WebUtils.success();
    }

    @PostMapping("changeParentBatch")
    @ApiOperation(value = "changeParentBatch", notes = "批量修改 AppPower 的 parentId、index 属性")
    public WebResult changeParentBatch(@RequestBody List<ObjectParentAndIndexChangeDto> list) {
        service.changeParentOrIndexBatch(list);
        return WebUtils.success();
    }

    @GetMapping("/child/{parentId}")
    @ApiOperation(value = "listChild", notes = "查询指定 parentId 属性的 AppPower")
    public WebResult listChild(@PathVariable("parentId") String parentId) {
        return WebUtils.success(service.queryByParentId(parentId));
    }

    @GetMapping("/childPage/{parentId}/{pageSize}/{pageIndex}")
    @ApiOperation(value = "listPageChild", notes = "查询指定 parentId 属性的 AppPower")
    public WebResult listPageChild(@PathVariable("parentId") String parentId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) {
        return WebUtils.success(service.queryPageByParentId(parentId, pageSize, pageIndex));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 AppPower")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 AppPower 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @GetMapping("listPowerResourceSet")
    @ApiOperation(value = "listPowerResourceSet", notes = "获取自定义权限资源集")
    public WebResult<List<PowerResourceSet>> listPowerResource(@RequestParam(required = false) String roleId, @RequestParam PowerTypeEnum typeEnum, @RequestParam int pageIndex, @RequestParam int pageSize) {
        List<PowerResourceSet> result = new ArrayList<>();

        switch (typeEnum) {
            case Spu:
                result.add(productService.queryResourcePower(roleId, pageIndex, pageSize));
                break;
            case Customer_Source:
                result.add(customerService.queryResourcePower(roleId, pageIndex, pageSize));
                break;
            case Order_Discount_Apply:
                result.add(orderService.queryResourcePower(roleId, pageIndex, pageSize));
                break;
            default:
                throw new BusinessException("不支持此类型的资源权限定义。");
        }
        return WebUtils.success(result);
    }

}